#include "CSem.h"
#include <commlib.h>
#include <taskMgr.h>

static int signal(struct _csem_* sem) {
	sem->sig_cnt++;
	return 0;
}

static int wait(volatile struct _csem_* sem) {
	if(sem->sig_cnt <= 0) return SEM_WAIT_FAILED;
    sem->sig_cnt--;
	return SEM_WAIT_OK;
}

int construct_sem(CSem* sem, long initial_count) {
	memset(sem, 0, sizeof(*sem));
	sem->wait = wait;
	sem->signal = signal;
	sem->sig_cnt = initial_count;
	return 0;
}

